package com.dg11185.nearshop.activity;

import com.android.nearshop.R;
import com.dg11185.nearshop.app.Constants;
import com.dg11185.nearshop.app.ViewUtils;
import com.wlper.lib.sys.Screen;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 带标题栏的Fragment模板。默认情况下，标题栏左部自带一个图标。
 * 
 * @author wanliang527
 * @date 2014-5-9
 */
public abstract class BaseFragment extends Fragment {

    protected final static int LEFT = 1;
    protected final static int MIDDLE = 2;
    protected final static int RIGHT = 3;

    private View mWholeView;
    private View mContentView;
    protected Activity mActivity;
    protected LayoutInflater mInflater;
    protected SharedPreferences mPreferences;
    protected ImageView iconView;
    protected TextView titleView;
    protected Dialog loadingDialog = null;
    protected static int mScreenWidth, mScreenHeight;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        mPreferences = PreferenceManager.getDefaultSharedPreferences(mActivity);
        Screen s = Screen.getInstance(mActivity);
        mScreenWidth = s.getScreenWidth();
        mScreenHeight = s.getScreenHeight();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mInflater = inflater;
//        mWholeView = mInflater.inflate(R.layout.fragment_template, null);
//        if (!initTopBanner())
//            defaultInit();
//        mContentView = setContentView();
//        if (mContentView != null)
//            ((ViewGroup) mWholeView).addView(mContentView);

        if (mWholeView == null) {
            mWholeView = mInflater.inflate(R.layout.fragment_template, null);
            if (!initTopBanner()) {
                defaultInit();
            }
            mContentView = setContentView();
            if (mContentView != null) {
                ((ViewGroup) mWholeView).addView(mContentView);
            }
        } else {
            ViewGroup parent = (ViewGroup) mWholeView.getParent();
            if (parent != null) {
                parent.removeAllViewsInLayout();
            }
        }
        return mWholeView;
    }

    /**
     * 默认初始化内容
     */
    protected void defaultInit() {
        iconView = new ImageView(mActivity);
        iconView.setBackgroundResource(R.drawable.icon_title);
        getPartView(LEFT).addView(iconView);
        ((LinearLayout.LayoutParams) iconView.getLayoutParams()).gravity = Gravity.CENTER_VERTICAL;

    }

    /**
     * 获取顶部标题栏的各个区域
     * 
     * @param pos
     *            LEFT、MIDDLE、RIGHT分别代表左中右三块区域
     * @return
     */
    public LinearLayout getPartView(int pos) {
        if (mWholeView == null)
            return null;
        switch (pos) {
            case LEFT:
                return (LinearLayout) mWholeView
                        .findViewById(R.id.llayout_left);

            case MIDDLE:
                return (LinearLayout) mWholeView
                        .findViewById(R.id.llayout_middle);

            case RIGHT:
                return (LinearLayout) mWholeView
                        .findViewById(R.id.llayout_right);

            default:
                return null;
        }
    }

    /**
     * 获取标题栏图标
     * 
     * @return
     */
    public ImageView getIconView() {
        return iconView;
    }

    /**
     * 设置标题栏图标
     * 
     * @param iconView
     */
    public void setIconView(ImageView iconView) {
        this.iconView = iconView;
    }

    public void setTitle(String title) {
        if (titleView == null) {
            titleView = new TextView(mActivity);
            titleView.setTextColor(getResources().getColor(R.color.white));
            titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP,
                    Constants.TITLE_FONT_SIZE);
            titleView.setGravity(Gravity.CENTER);
            getPartView(MIDDLE).addView(titleView, ViewUtils.WRAP_MATCH);
        }
        titleView.setText(title);
    }

    /**
     * 子类在此创建具体内容视图，并进行控件初始化
     * 
     * @return 初始化完成的视图
     */
    abstract public View setContentView();

    /**
     * 针对顶部标题栏的相关设置都放在该方法中进行。
     * 
     * @return 若不添加任何初始化代码，使用默认的设置，返回false；否则返回true。
     */
    abstract public boolean initTopBanner();

    /**
     * 显示加载对话框
     */
    public void showLoading() {
        if (loadingDialog == null) {
            loadingDialog = ViewUtils.createLoadingDialog(mActivity);
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.setContentView(ViewUtils.createDialogView(mActivity));
            loadingDialog.show();
        }
    }

    /**
     * 隐藏加载对话框
     */
    public void hideLoading() {
        if (loadingDialog != null && loadingDialog.isShowing())
            loadingDialog.dismiss();
    }
}
